package com.atguigu.tingshu.dispatch.job;

import com.atguigu.tingshu.search.client.SearchFeignClient;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class DispatchHandler {


    @XxlJob("myJob")
    public void myJob() {
        String jobParam = XxlJobHelper.getJobParam();
        log.info("任务执行:{}", jobParam);
        int i = 1 / 0;
    }


    @XxlJob("myJobReturn")
    public ReturnT myJobReturn() {
        try {
            String jobParam = XxlJobHelper.getJobParam();
            log.info("任务执行:{}", jobParam);
            int i = 1 / 0;
        } catch (Exception e) {
            log.error("异常：{}", e);
            return ReturnT.FAIL;
        }
        return ReturnT.SUCCESS;
    }


    @Autowired
    private SearchFeignClient searchFeignClient;


    /**
     * 每天凌晨1点触发更新Redis排行榜数据
     */
    @XxlJob("updateAlbumRanking")
    public void updateAlbumRanking() {
        log.info("定时执行热门专辑更新");
        searchFeignClient.updateLatelyAlbumRanking();
    }

    @Autowired
    private UserFeignClient userFeignClient;
    /**
     * 每天凌晨1点触发更新VIP过期状态
     */
    @XxlJob("updateVipExpireStatus")
    public void updateVipExpireStatus(){
        userFeignClient.updateVipExpireStatus();
    }

    @XxlJob("shardjob")
    public void shardjob(){
        int shardTotal = XxlJobHelper.getShardTotal();
        int shardIndex = XxlJobHelper.getShardIndex();
        log.info("总分片数：{}", shardTotal);
        log.info("当前分片索引：{}", shardIndex);
    }
}
